package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.LocationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.Map;

@DubboService
public class LocationServiceImpl implements LocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询用户地理位置
    @Override
    public Map findLocation(Long userId) {
        //1.创建条件
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        //2.查询
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        GeoJsonPoint geoJsonPoint = userLocation.getLocation();
        Map map = new HashMap<>();
        double x = geoJsonPoint.getX();
        double y = geoJsonPoint.getY();
        map.put("x",x );
        map.put("y",y );
        //3.返回
        return map;
    }
}
